import { SampleMethodProps, SampleStepInfo, SampleSortField, SystemExtractType } from "@/pages/auditSampling/typing";
import { forwardRef, useContext } from "react";
import { CheckBox, List, NumberBox, RadioGroup, SelectBox } from "devextreme-react";
import { consts } from "@/pages/auditSampling/consts";
import { getNumToChinese, leftVerticalDivStyle } from "../NonStatSampleService";
import { SampleStepInfoContext } from "../../samplingRecordPopup";
import { getSystemSampleNos } from "./SystemSampleService";
/**
 * 系统抽样 参数
 */
export const SystemSample = forwardRef((props: SampleMethodProps, ref) => {
    const systemSampleParams = props.sampleParameterValues?.systemSampleParams || {};
    const sampleSortFields = systemSampleParams.sampleSortFields || [];//一个新的,排序字段

    //审计抽样，凭证表，2.确定抽样参数，系统抽样，排序关键字，需增加借或贷方金额，可用“借方金额绝对值+贷方金额绝对值”排序

    //上级传入的抽样参数
    const sampleStepInfo: SampleStepInfo = useContext(SampleStepInfoContext);
    let sampleAllFieldsArray = [...(sampleStepInfo.projectOverallProps?.curSampleTable?.sampleAllFieldsArray || [])]
    //增加一个空的
    sampleAllFieldsArray.unshift({
        fieldName: "",
        fieldTitle: "",
        guid: "1",
        tableName: "",
        isKey: false,
        isNum: 0,
        align: "",
        displayWidth: 0,
        displayFormat: "",
        orderIndex: 0,
        visible: true,
        enabled: true,
        remarks: ""
    })
    
    //增加凭证的借方或贷方金额
    if (props.projectOverallProps?.curSampleTable?.isVoucher) {
        const index = sampleAllFieldsArray.findIndex(item => item.fieldName == 'JFJE');
        if (index > -1) {
            const jfjeField = sampleAllFieldsArray[index];
            sampleAllFieldsArray.splice(index, 0, {
                ...jfjeField,
                fieldName: consts.JFJE_DFJE_Field,
                fieldTitle: consts.JFJE_DFJE_Title,
            });
        }
    }
    /* 
    function updateSortValues() {
        props.setSampleParameterValues({
            ...props.sampleParameterValues,
            systemSampleParams: {
                ...props.sampleParameterValues.systemSampleParams,
                sampleSortFields: sampleSortFields
            }
        })
    } */
    function updateSystemSampleParams() {
        props.setSampleParameterValues({
            ...props.sampleParameterValues,
            systemSampleParams: systemSampleParams
        })
    }

    return <>

        <div className="itemBoxDispalyLeft">
            <div className="itemBoxDispalyLeft"
                style={{ marginLeft: 0, height: 125, border: '1px solid #e5e5e5', borderRadius: 5, width: '500px' }}  >
                <div style={leftVerticalDivStyle}>
                    <div>排</div>
                    <div>序</div>
                </div>
                <List dataSource={sampleSortFields}
                    focusStateEnabled={false}
                    style={{ paddingLeft: 1, paddingTop: 5 }}
                    itemRender={(item: SampleSortField, index: number) => {
                        // 是否绝对值
                        const isNum = sampleAllFieldsArray.some(item2 => sampleSortFields[index].fieldName == item2.fieldName && item2.isNum);
                        if (!isNum) {
                            sampleSortFields[index].isAbs = false;
                        }
                        return (
                            <div className="itemBoxDispalyLeft">
                                <div>{`${getNumToChinese(index + 1)}关键字　`}</div>
                                <SelectBox
                                    style={{ marginTop: -5, marginRight: 5 }}
                                    height={30}
                                    width={120}
                                    dataSource={sampleAllFieldsArray}
                                    valueExpr={'fieldName'}
                                    displayExpr={'fieldTitle'}
                                    value={item.fieldName}
                                    dropDownOptions={{ width: 200 }}
                                    searchEnabled={true}
                                    noDataText="请选择"
                                    onValueChanged={(e) => {
                                        sampleSortFields[index].fieldName = e.value
                                        updateSystemSampleParams();
                                    }}
                                />
                                <RadioGroup
                                    dataSource={consts.sampleSortList}
                                    value={item.sortType}
                                    layout="horizontal"
                                    valueExpr={'value'}
                                    displayExpr={'text'}
                                    onValueChanged={(e) => {
                                        sampleSortFields[index].sortType = e.value
                                        updateSystemSampleParams();
                                    }}
                                />
                                <CheckBox
                                    style={{ marginLeft: '10px' }}
                                    value={item.isAbs}
                                    text="绝对值"
                                    disabled={!isNum || item.fieldName == consts.JFJE_DFJE_Field}
                                    onValueChanged={(e) => {
                                        sampleSortFields[index].isAbs = e.value
                                        updateSystemSampleParams();
                                    }}
                                />
                            </div>
                        )
                    }}
                />
            </div>

            <div className="itemBoxDispalyLeft"
                style={{ marginLeft: 10, height: 125, border: '1px solid #e5e5e5', borderRadius: 5, width: 'calc(100% - 500px)' }}  >
                <div style={leftVerticalDivStyle}>
                    <div>抽</div>
                    <div>取</div>
                </div>
                <div className="" style={{ width: '100%', paddingLeft: 10, paddingTop: 10 }}>
                    <RadioGroup
                        dataSource={consts.extractMethodArray}
                        value={systemSampleParams.extractType}
                        //layout="horizontal"
                        valueExpr={'value'}
                        displayExpr={'text'}
                        onValueChanged={(e) => {
                            systemSampleParams.extractType = e.value;
                            updateSystemSampleParams();
                        }}
                    />
                    <div className="itemBoxDispalyLeft" style={{ marginTop: 5, marginLeft: 0 }}>
                        <div style={{ marginTop: 5 }}>抽取间隔：</div>
                        <NumberBox
                            style={{ height: 30, width: 90 }}
                            hint={systemSampleParams.interval + ''}
                            value={systemSampleParams.interval}
                            onValueChanged={(e) => {
                                systemSampleParams.interval = e.value;
                                systemSampleParams.extractNumber = 0;
                                if (props.sampleParameterValues.toBeTestSampleSize) {
                                    const sampleNos = getSystemSampleNos(
                                        props.sampleParameterValues.toBeTestSampleSize || 0,
                                        e.value,
                                        systemSampleParams?.extractType == SystemExtractType.asc
                                    );
                                    systemSampleParams.extractNumber = sampleNos.length;
                                }

                                updateSystemSampleParams();
                            }}
                            min={0}
                            showSpinButtons={true}
                        />
                        <div style={{ marginTop: 5, marginLeft: 5 }}>数量：{props.sampleParameterValues.systemSampleParams?.extractNumber || 0}</div>
                    </div>

                </div>
            </div>

        </div>
    </>
})